<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\Gift;
use App\Model\SettingAllocate;
use App\Model\UserProfile;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;

class SettingAllocateController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('名义礼物分配管理');
        $content->body($this->grid());
        return $content;
    }

    public function edit($id)
    {
        $content = new Content();
        $content->header('编辑');
        $content->body($this->form()->edit($id));
        return $content;
    }

    public function create()
    {
        $content = new Content();
        $content->header('创建');
        $content->body($this->form());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new SettingAllocate());
        $grid->model()->orderBy('priority', 'desc');
        $grid->priority('优先级')->sortable()->editable();
        $grid->male_cate('男用户分类')->display(function ($data) {
            return SettingAllocate::MALE_CATE[$data] ?? '-';
        });
        $grid->host_cate('分配的女用户分类')->display(function ($data) {
            return UserProfile::HOST_CATE[$data] ?? '-';
        });
        $grid->gift_info('礼物信息')->display(function ($data) {
            $gift = Gift::find($this->gift_id);
            return $gift['name'] . "（{$gift['coin']}金币）";
        });
        $grid->gift_num('礼物总数限制');
        $grid->single_gift_num('单次上线推送礼物数');
        $grid->gift_expire_time('礼物过期时间（秒）');
        $grid->flag('状态')->display(function ($data) {
            return $data == 1 ? '有效' : '无效';
        });
        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new SettingAllocate());
        $form->select('male_cate', '男用户类别')->options(SettingAllocate::MALE_CATE)->required();
        // $form->textarea('male_uids', '指定男用户ID（如配置，优先级最高）')->rows(10);
        $form->select('host_cate', '女用户类别')->options(UserProfile::HOST_CATE)->required();
        $form->select('gift_id', '分配礼物ID')->options(Gift::getList(2))->required();
        $form->text('gift_num', '总礼物数')->default(1)->required();
        $form->text('single_gift_num', '单次上线推送礼物数')->default(1)->required();
        $form->text('gift_expire_time', '礼物过期时间（秒）')->default(0)->required();
        $form->text('priority', '优先级(越高越优先，最大255)')->default(1)->required();
        // $form->multipleSelect('guild', '女用户代理（类别为代理女用户才有效）')->options('/get_guilds')->required();
        // $form->textarea('host_uids', '指定女用户ID（如配置，优先级最高）')->rows(10);
        $form->select('flag', '是否有效')->options([1 => '有效', 0 => '无效'])->default(1)->required();
        return $form;
    }
}
